package com.rech.xbexpress3;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rech.adapter.LogisticAdapter;
import com.rech.adapter.adapterInterface.OnItemClickListener;
import com.rech.module.LogisticModule;
import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 选择快递页
 * Created by sunshine on 2020/9/30
 */
public class ChooseLogicActivity extends AppCompatActivity {
    private String wl_id = "";
    private String kdy_id = "";
    private Timer countTimer;
    private TextView djs;
    private int jjcount = 100;
    private LogisticAdapter mAdapter;
    private List<LogisticModule> logisticModule = new ArrayList<>();
    private RecyclerView rvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation", "");
        if (orientation.equals("landscape")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        //eventbus
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        setContentView(R.layout.activity_choose_logic);
        getSupportActionBar().hide();

        djs = findViewById(R.id.djs);
        rvContent = (RecyclerView) findViewById(R.id.rv_content);
        logisticModule = (ArrayList<LogisticModule>) getIntent().getSerializableExtra("logisticModule");

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mAdapter = new LogisticAdapter(this, logisticModule);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                for (int i = 0; i < logisticModule.size(); i++) {
                    if (i != position) {
                        logisticModule.get(i).setChecked(false);
                    } else {
                        logisticModule.get(i).setChecked(true);
                    }
                }
                mAdapter.notifyDataSetChanged();

                kdy_id = logisticModule.get(position).getAdminid();
                wl_id = logisticModule.get(position).getId();

                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                editor.putString("kdy_id",kdy_id);
                editor.putString("wl_id",wl_id);
                editor.apply();

                Intent intent = new Intent();
                intent.putExtra("wl_id", wl_id);
                setResult(1010, intent);
                Exit();
                finish();
            }
        });
        rvContent.setLayoutManager(manager);
        rvContent.setAdapter(mAdapter);

        countTimer=new Timer();
        countTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                jjcount--;
                Message message = new Message();
                message.what=20000;
                EventBus.getDefault().post(message);
                if(jjcount < 1){
                    //OtherUtil.restartApplication(Jj2Activity.this);
                    Exit();
                    finish();
                }
            }
        },0,1000);

        TextView tv_back = findViewById(R.id.tv_back);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Exit();
                finish();
            }
        });

        TextView tv_version = findViewById(R.id.tv_version);
        TextView tv_waiter = findViewById(R.id.tv_waiter);
        try {
            tv_version.setText("v" + OtherUtil.getVersion(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phone = sharedPreferences.getString("CabinetType_phone","");
        tv_waiter.setText("客服电话:" + phone);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message){
        Intent intent = new Intent();
        switch(message.what){
            case 10402:
                //handleKDLjmTask(message.obj);
                break;
            case 10403:
                //Toast.makeText(Kd3Activity.this, "揽件码错误", Toast.LENGTH_SHORT).show();
                break;
            case 20000:
                handleDJS();
                break;
        }
    }

    private void handleDJS(){
        djs.setText(String.valueOf(jjcount));
    }

    private void Exit(){
        countTimer.cancel();
        EventBus.getDefault().unregister(this);
    }
}

